#include "app_def.h"
#include "upnp.h"
#include "media_renderer_device.h"
#include <thread>

std::string url;

void on_action(IAction::shared action)
{
    switch (action->get_type())
    {
    case ENUM_CONTROL_ACTION_TYPE_SET_AV_TRANSPORT_URI:
    {
        auto ac = std::dynamic_pointer_cast<SetAvTransportUriAction>(action);
        url = ac->get_current_uri();
        break;
    }
    case ENUM_CONTROL_ACTION_TYPE_PLAY:
    {
        std::string cmd = std::string("vlc ") + url;
        std::string url_tmp = url;
        std::thread t([cmd, url_tmp]() {
            system(cmd.c_str());
        });
        t.detach();
        
        break;
    }
    case ENUM_CONTROL_ACTION_TYPE_STOP:
    {
        system("killall vlc");
    }
    };
}

int main()
{
    auto upnp = std::make_shared<Upnp>();
    MediaRendererDevice::shared dev = std::make_shared<MediaRendererDevice>("test_renderer");
    dev->set_callback(std::bind(&on_action, std::placeholders::_1));
    upnp->add_device(dev);
    upnp->start();

    getchar();
    return 0;
}